/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		StringTable.cpp

					E-mail:		arise_engine@mail.ru

created:			30.01.2008
last changes:		05.02.2008
*****************************************************************************************/
#include "stdafx.h"
#include "..\engine\EngineTokens.h"

#define STRINGTABLE_NAME_DEFAULT	"__arise__default__"
#define STRINGTABLE_STRING_ERROR	"empty string at "


StringTable::StringTable()
{
}
StringTable::~StringTable()
{
}


bool StringTable::LoadFromFile(FileData* file, int max_length)
{
	if(!file)
	{
		return false;
	}

	if(max_length <= 0) max_length = STRING_TABLE_MAX_LENGTH;

	TextParser parser(file, max_length);

	TCHAR* token = 0;

	m_strings[STRINGTABLE_NAME_DEFAULT] = STRINGTABLE_STRING_ERROR + file->GetName();

	while((token = parser.GetToken()) && !parser.IsFeof()) 
	{
		std::string name = token;

		if(m_strings.find(name) != m_strings.end())
		{
			message_box(file->GetName().c_str(), "Duplicate name '%s'", name.c_str());
		}
		
		token = parser.GetToken();
		if(parser.Compare(token, ID_EQUAL))
		{
			std::string string = parser.GetStringWithoutQuotation();
			if(string.empty()) 
				message_box(file->GetName().c_str(), "Empty string at mark '%s'", name.c_str());
			m_strings[name] = string;
		}
		else
		{
			message_box(file->GetName().c_str(), "Invalid structure of string table, '=' is expected: \n\n'%s'", token);
		}
	}

	return true;
}


const std::string& StringTable::GetString(const std::string &name)
{
	if(m_strings.find(name) == m_strings.end())
	{
		return m_strings[STRINGTABLE_NAME_DEFAULT];	//	error, string not found
	}
	return m_strings[name];
}


